import com.lagou.minicat.utils.HttpProtocolUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：PACKAGE_NAME.MiniCatMain
 * @作者 mengwei
 * 创建日期：2021/5/12 16:26
 */
public class MiniCatMain {

    /**
     * 默认端口 8080
     */
    private static final  int port = 18081;
    public static void main(String[] args) throws IOException {
        start02();


    }

    /**
     * 1.0 返回固定的字符串
     * @throws IOException
     */
    private static void start01() throws IOException {

        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("====>>>Minicat "+ port);
        while (true){

            Socket socket = serverSocket.accept();
            String data = "Hello Minicat!";
            String resp = HttpProtocolUtils.getHttpHeader200(data.getBytes().length)+data;
            socket.getOutputStream().write(resp.getBytes());
            socket.close();
        }
    }

    /**
     * 版本2
     * 封装 response、request对象，返回静态HTML页面
     * @throws IOException
     */
    private static void start02() throws IOException {

        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("====>>>Minicat "+ port);
        while (true){

            Socket socket = serverSocket.accept();
            String data = "Hello Minicat!";
            String resp = HttpProtocolUtils.getHttpHeader200(data.getBytes().length)+data;
            socket.getOutputStream().write(resp.getBytes());
            socket.close();
        }
    }


}
